package com.corpdata.system.org.service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.corpdata.common.api.activemq.Producer;
import com.corpdata.common.result.DataGrid;
import com.corpdata.common.result.Result;
import com.corpdata.common.result.util.ResultUtil;
import com.corpdata.system.org.dao.UserMapper;
import com.corpdata.system.org.entity.User;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private Producer producer;
	
	public User selectByUid(String userId){
		return userMapper.selectByUid(userId);
	}
	
	/**
	 * 查询数据列表
	 * 
	 * @param dt
	 * @return
	 */
	@Cacheable(value="users",cacheManager = "redisCacheManagerString")
	public DataGrid<User> selectByPage(DataGrid dg) {
		PageHelper.startPage(dg.getPage(), dg.getSize());
		Page<User> rows = userMapper.selectByPage(dg.getSearchParams());
		dg.setTotalAndRows(rows.getTotal(), rows);
		System.out.println("select user data...");
		return dg;
	}
	
	public List<User> selectAll(){
		System.out.println("select user data...");
		return userMapper.selectByPage(null);
	}

	/**
	 * 新增数据
	 * 
	 * @param user
	 * @return
	 */
	public Result create(User user) {
		//userMapper.create(user);
		for(int i=0;i<20;i++){
			producer.sendMsg("queue.mail", "createuser:"+i);
		}
		return ResultUtil.success();
	}

	/**
	 * 修改数据
	 * 
	 * @param user
	 * @return
	 */
	public Result update(User user) {
//		if (userMapper.update(user) > 0) {
//			return ResultUtil.success();
//		} else {
//			return ResultUtil.fail();
//		}
		return null;
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 * @return
	 */
	@CacheEvict(value="users", allEntries=true)
	public Result delete(String id) {
//		orgUserMapper.delete(id);
		return ResultUtil.success();
	}
}
